﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using FRS.ExceptionLog;

namespace FRS.FileProcessor
{
    /// <summary>
    /// FRS Scene Document(后缀名为.frsxd)只会保存AxSceneControl.Scene的相关信息，
    /// 并不包含有效的Scene Document，所以不能被ArcScene打开！！！
    /// </summary>
    public static class FRSxDocumentProcessor
    {
        /// <summary>
        /// 保存为.frsxd文件
        /// </summary>
        /// <returns>如果保存成功则返回TRUE，否则返回FALSE</returns>
        public static bool SaveFrsxFile(this AxSceneControl axSceneControl)
        {
            SaveFileDialog sfDialog = new SaveFileDialog();
            sfDialog.Title = "Save Scene Document";
            sfDialog.Filter = "FRS Scene Document(*.frsxd)|*.frsxd";
            DialogResult dr = sfDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                try
                {
                    IMemoryBlobStream mbStream = new MemoryBlobStreamClass();
                    IObjectStream objectStream = new ObjectStreamClass();
                    IPersistStream persistStream = axSceneControl.Scene as IPersistStream;

                    objectStream.Stream = mbStream;
                    persistStream.Save(objectStream, 1);
                    mbStream.SaveToFile(sfDialog.FileName);

                    MessageBox.Show(string.Format("文件：\n{0}\n已保存成功!", sfDialog.FileName));
                    return true;
                }
                catch (Exception ex)
                {
                    ExceptionLogRecorder.WriteExceptionLog(ex, string.Format("文件：“{0}”保存失败!", sfDialog.FileName));
                    MessageBox.Show(string.Format("文件：\n{0}\n保存失败!", sfDialog.FileName));
                    return false;
                }
            }
            else
            {
                MessageBox.Show("用户未选择保存文件");
                return false;
            }
        }
        /// <summary>
        /// 加载.frsxd文件
        /// </summary>
        /// <returns>加载成功则返回TRUE，否则返回FALSE</returns>
        public static bool LoadFrsxFile(this AxSceneControl axSceneControl)
        {
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.Title = "Load Scene Document";
            ofDialog.Filter = "FRS Scene Document(*.frsxd)|*.frsxd";
            DialogResult dr = ofDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                try
                {
                    IMemoryBlobStream mbStream = new MemoryBlobStreamClass();
                    IObjectStream objectStream = new ObjectStreamClass();
                    IPersistStream persistStream = axSceneControl.Scene as IPersistStream;

                    objectStream.Stream = mbStream;
                    mbStream.LoadFromFile(ofDialog.FileName);
                    persistStream.Load(objectStream);

                    MessageBox.Show(string.Format("文件：\n{0}\n已加载成功!", ofDialog.FileName));
                    return true;
                }
                catch (Exception ex)
                {
                    ExceptionLogRecorder.WriteExceptionLog(ex, string.Format("文件：“{0}”加载失败!", ofDialog.FileName));
                    MessageBox.Show(string.Format("文件：\n{0}\n加载失败!", ofDialog.FileName));
                    return false;
                }
            }
            else
            {
                MessageBox.Show("用户未选择加载文档");
                return false;
            }
        }
    }
}
